<?php

class User extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	public function rules() {
		return array(
			array('username', 'unique', 'message' => 'User already exists'),
		);
	}

	public function safeAttributes() {
		return array('username', 'email', 'profile');
	}

	public function relations() {
		return array(
			'torrents'=>array(self::HAS_MANY, 'Torrent', 'uploader_id'),
			'peer_on' => array(self::HAS_MANY, 'Peer', 'user_id')
		);

	}

	public function getRatio() {
		return ($this->downloaded > 0) ? $this->uploaded / $this->downloaded : 'Inf.';
	}

	public function getAvailableRoles() {
		return array('user', 'uploader', 'moderator', 'administrator');
	}

	protected function afterDelete() {
		$this->dbConnection->createCommand('DELETE FROM authassignment WHERE userid='.$this->id)->execute();
	}


}